package com.example.administrator.im.ui.activity;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.calendar.utils.LunarCalendar;
import com.example.administrator.im.R;
import com.example.administrator.im.view.CustomCalendar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CalendarActivity extends AppCompatActivity {


    private CustomCalendar cal;
    private Button location;
    private Button sign;
    private ImageView now_day;

    private String date;
    private CustomCalendar setMonth;
    private CustomCalendar getMonthStr;

    private TextView tv_nl_day;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        getWindow().setStatusBarColor(Color.TRANSPARENT);

        setContentView(R.layout.activity_calendar);

        //获取定位打卡按钮控件
        cal = (CustomCalendar) findViewById(R.id.cal);
        now_day = (ImageView) findViewById(R.id.now_day);
        tv_nl_day = (TextView) findViewById(R.id.nl_day);

        final LunarCalendar calendar = new LunarCalendar();

        //日期格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        //获取当前时间
        Date nowdate = new Date(System.currentTimeMillis());
        //设置时间格式为指定格式
        date = format.format(nowdate);
        //获取年份
        String str[] = date.split("年");
        final String year = str[0];
        //获取月份
        String str1[] = str[1].split("月");
        final String month = str1[0];
        String str2[] = str1[1].split("日");
        final String day = str2[0];

        //将年月转换为int型
        year_count = Integer.valueOf(year);
        month_count = Integer.valueOf(month);
        final int month_ = month_count;

        //TODO 模拟请求当月数据
        final List<DayFinish> list = new ArrayList<>();
        for (int i = 0; i < 31; i++) {
            String w = calendar.getLunarString(year_count, month_count, i + 1);
            String nl_day = w.split("月")[1].substring(0, 2);
            DayFinish dayFinish = new DayFinish(i + 1, nl_day);
            list.add(dayFinish);
        }

        tv_nl_day.setText(calendar.getLunarString(year_count, month_count, Integer.valueOf(day)));

        //定位到当前日期
        now_day.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //日期格式
                SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
                //获取当前时间
                Date nowdate = new Date(System.currentTimeMillis());
                //设置时间格式为指定格式
                date = format.format(nowdate);
                //设置日期数据
                cal.setRenwu(date, list);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv_nl_day.setText(calendar.getLunarString(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day)));
                    }
                });
            }
        });


        cal.setRenwu(date, list);
        cal.setOnClickListener(new CustomCalendar.onClickListener() {
            @Override
            public void onLeftRowClick() {
                cal.monthChange(-1);
                new Thread() {
                    @Override
                    public void run() {
                        CalendarActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                list.clear();
                                int month__ = getMonth(-1);
                                for (int i = 0; i < 31; i++) {
                                    list.add(getDayFinish(calendar, month__, i + 1));
                                }
                                cal.setRenwu(list);
                                if (month_ == month_count) {
                                    tv_nl_day.setText(calendar.getLunarString(year_count, month_count, Integer.valueOf(day)));
                                }
                            }
                        });
                    }
                }.start();
            }


            @Override
            public void onRightRowClick() {
                cal.monthChange(1);
                new Thread() {
                    @Override
                    public void run() {
                        CalendarActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                int month__ = getMonth(1);
                                list.clear();
                                for (int i = 0; i < 31; i++) {
                                    list.add(getDayFinish(calendar, month__, i + 1));
                                }
                                cal.setRenwu(list);
                                if (month_ == month_count) {
                                    tv_nl_day.setText(calendar.getLunarString(year_count, month_count, Integer.valueOf(day)));
                                }
                            }
                        });

                    }
                }.start();
            }

            //页面吐司
            @Override
            public void onTitleClick(String monthStr, Date month) {
//                Toast.makeText(CalendarActivity.this, "点击了标题：" + monthStr, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onWeekClick(int weekIndex, String weekStr) {
//                Toast.makeText(CalendarActivity.this, "今天是星期" + weekStr, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDayClick(final int day, String dayStr, DayFinish finish) {
//                Toast.makeText(CalendarActivity.this, "当前日期：" + dayStr, Toast.LENGTH_SHORT).show();
                //获取年份
                String str[] = dayStr.split("年");
                final String year = str[0];
                //获取月份
                String str1[] = str[1].split("月");
                final String month = str1[0];
                String str2[] = str1[1].split("日");
                final String day_ = str2[0];

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv_nl_day.setText(calendar.getLunarString(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day_)));
                    }
                });
            }
        });

    }

    private DayFinish getDayFinish(LunarCalendar calendar, int month__, int i) {
        String w = calendar.getLunarString(year_count, month__, i);
        String nl_day = w.split("月")[1].substring(0, 2);
        DayFinish dayFinish = new DayFinish(i, nl_day);
        return dayFinish;
    }

    private int getMonth(int flag) {
        month_count += flag;
        if (month_count == 0) {
            month_count = 12;
            year_count -= 1;
        } else if (month_count == 13) {
            month_count = 1;
            year_count += 1;
        }
        return month_count;
    }

    private int year_count = 0;
    private int month_count = 0;

    public class DayFinish {
        public int day;
        public String nl_day;
        public int all;
        public int finish;

        public DayFinish(int day, int finish, int all) {
            this.day = day;
            this.all = all;
            this.finish = finish;
        }

        public DayFinish(int day, String nl_day) {
            this.day = day;
            this.nl_day = nl_day;
        }
    }


}
